<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style media="screen">
        #canvas{
            background: #fff;
            display: block;
            margin: 100px auto;
        }
        body {
            background: #000;
        }
    </style>
</head>
<body>

    <img src="" alt="">
    <canvas id="canvas" width="500" height="100"></canvas>
    <script>
        let canvas = document.querySelector('#canvas');
        let ctx = canvas.getContext('2d');
        let count = 0;
        let n = 0;
        let x = 50;
        let pause = false;
        let img = new Image();
        img.src = './fish1.png';
        img.onload = function() {
            requestAnimationFrame(next);
        }

        function next() {
            if (!pause) {
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                count++;
                ctx.drawImage(img, 0, n * 36, 55, 36, x, 50, 55, 36);
                x += 2;
                if (count % 6 == 0) {
                    n++;
                    if (n == 4) n = 0;
                }
            }
            requestAnimationFrame(next);
        }

        document.addEventListener('click', ()=>{
            pause = !pause;
        })

    </script>
</body>
</html>
